package com.example.mybatisplusdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.mybatisplusdemo.constant.ApiConstants;
import com.example.mybatisplusdemo.entity.User;
import com.example.mybatisplusdemo.model.ApiResult;
import com.example.mybatisplusdemo.model.query.PageQuery;
import com.example.mybatisplusdemo.model.query.UserQuery;
import com.example.mybatisplusdemo.model.vo.UserVO;
import com.example.mybatisplusdemo.service.UserService;
import com.example.mybatisplusdemo.util.PageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author Xu Haidong
 * @date 2018/3/29
 */
@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    /**
     * 新增
     *
     * @return
     */
    @PutMapping
    public ApiResult add() {
        try {
            boolean res = userService.insert(new User("任志强", 59L, 1));
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, res);
        } catch (Exception e) {
            logger.error("{}_{}", "", e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }

    /**
     * 根据主键修改
     *
     * @param user
     * @return
     */
    @PutMapping("update")
    public ApiResult update(@RequestBody User user) {
        try {
            boolean res = userService.updateById(user);
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, res);
        } catch (Exception e) {
            logger.error("{}_{}", JSONObject.toJSONString(user), e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }

    }

    /**
     * 批量删除
     * 前端直接传参JSON数组如：["1","2","3"]
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public ApiResult remove(@RequestBody String[] ids) {
        try {
            boolean res = userService.deleteBatchIds(Arrays.asList(ids));
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, res);
        } catch (Exception e) {
            logger.error("{}_{}", JSONObject.toJSONString(ids), e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public ApiResult getById(@RequestParam("id") String id) {
        try {
            User user = userService.selectById(id);
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, user);
        } catch (Exception e) {
            logger.error("{}_{}", id, e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }

    /**
     * 分页查询
     *
     * @param pageQuery
     * @return
     */
    @PostMapping("/listUserByPage")
    public ApiResult listUserByPage(@RequestBody PageQuery pageQuery) {
        try {
            Page<User> list = userService.selectPage(PageUtils.pageQueryToPage(pageQuery));
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, list);
        } catch (Exception e) {
            logger.error("{}_{}", JSONObject.toJSONString(pageQuery), e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }

    /**
     * 新增，再查询
     *
     * @param userQuery
     * @return
     */
    @PostMapping("/listBizUserByPage")
    public ApiResult listBizUserByPage(@RequestBody UserQuery userQuery) {
        PageQuery pageQuery = userQuery.getPageQuery();
        try {
            Page<User> list = userService.complexBiz(PageUtils.pageQueryToPage(pageQuery), userQuery.getUser());
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, list);
        } catch (Exception e) {
            logger.error("{}_{}", JSONObject.toJSONString(userQuery), e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }

    @PostMapping("/listWithRole")
    public ApiResult listWithRole() {
        try {
            List<UserVO> list = userService.listWithRole();
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, list);
        } catch (Exception e) {
            logger.error("{}_{}", "", e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }

    @PostMapping("/listAll")
    public ApiResult listAll() {
        try {
            List<User> list = userService.listAll();
            return new ApiResult(ApiConstants.CODE.SUCCESS, ApiConstants.MSG.SUCCESS, list);
        } catch (Exception e) {
            logger.error("{}_{}", "", e.getMessage(), e);
            return new ApiResult(ApiConstants.CODE.FAIL, ApiConstants.MSG.FAIL, e.getMessage());
        }
    }
}
